class Solution {
    //岛屿周长
    //数学(陆地个数*4 - 重复边*2)
    //重复边=只用看上 + 左（不然就重复两遍）
    public int islandPerimeter(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int count = 0;
        int cover = 0;

        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if(grid[i][j] == 1) {
                    count++;
                    //上
                    if(i-1>=0 && grid[i-1][j]==1) cover++;

                    //左
                    if(j-1>=0 && grid[i][j-1]==1) cover++;
                }
            }
        }

        return 4*count-cover*2;
    }
}